import { userGetUserdetail, userSearchUserByKeyword } from "../../api/userService"

Page({
  data: {
    searchValue: "",
    searchResults: [],
    loading: false,
    isEmpty: true,
    showUserDetail: false,
    userDescription: "",
    userWeChatId: "",
    userQQId: "",
    // 分页相关
    page: 1,
    pageSize: 5,
    hasMore: true,
    loadingMore: false,
    total: 0,
    // 当前搜索的关键词
    currentKeyword: "",
  },

  onLoad() {
    // 页面加载时的初始化逻辑
  },

  onSearchInput(event) {
    this.setData({
      searchValue: event.detail,
    })
  },

  async onSearch(event) {
    const keyword = event.detail

    // 重置分页数据
    this.setData({
      page: 1,
      hasMore: true,
      searchResults: [],
      currentKeyword: keyword,
      total: 0,
    })

    // 显示加载状态
    this.setData({
      loading: true,
      isEmpty: false,
    })

    await this.fetchSearchResults()
  },

  // 获取搜索结果
  async fetchSearchResults() {
    const { currentKeyword, page, pageSize, searchResults } = this.data

    if (!currentKeyword) {
      this.setData({
        isEmpty: true,
        loading: false,
      })
      return
    }

    try {
      // 如果是加载更多，显示加载更多状态
      if (page > 1) {
        this.setData({
          loadingMore: true,
        })
      }

      // 调用后端请求
      const result = await userSearchUserByKeyword(
        this.data.currentKeyword,
        this.data.page, 
        this.data.pageSize
      )

      if (result.code === 200) {
        const newResults = result.data.records || []
        const total = result.data.total || 0

        // 更新数据
        this.setData({
          searchResults: page === 1 ? newResults : [...searchResults, ...newResults],
          total: total,
          loading: false,
          loadingMore: false,
          isEmpty: page === 1 && newResults.length === 0,
          hasMore: newResults.length >= pageSize, // 如果返回的数据量等于或大于pageSize，说明可能还有更多数据
        })
      } else {
        this.setData({
          loading: false,
          loadingMore: false,
          isEmpty: page === 1 && searchResults.length === 0,
        })
        wx.showToast({
          title: "搜索失败，请重试",
          icon: "none",
        })
      }
    } catch (error) {
      console.error("搜索失败:", error)
      this.setData({
        loading: false,
        loadingMore: false,
        isEmpty: page === 1 && searchResults.length === 0,
      })
      wx.showToast({
        title: "搜索失败，请重试",
        icon: "none",
      })
    }
  },

  // 加载更多数据
  loadMoreData() {
    if (this.data.hasMore && !this.data.loadingMore && !this.data.loading) {
      this.setData({
        page: this.data.page + 1,
      })
      this.fetchSearchResults()
    }
  },

  // 监听页面滚动到底部
  onReachBottom() {
    this.loadMoreData()
  },

  onCancel() {
    this.setData({
      searchValue: "",
      searchResults: [],
      isEmpty: true,
      loading: false,
      page: 1,
      hasMore: true,
      total: 0,
      currentKeyword: "",
    })
  },

  // 处理联系用户事件
  async handleContact(e) {
    const { userId } = e.detail

    try {
      const result = await userGetUserdetail(userId)
      if (result.code === 200) {
        this.setData({
          userDescription: result.data.description || "暂无简介",
          userWeChatId: result.data.wechatId || "未设置",
          userQQId: result.data.qqId || "未设置",
          showUserDetail: true,
        })
      } else {
        wx.showToast({
          title: "获取用户信息失败",
          icon: "none",
        })
      }
    } catch (error) {
      console.error("获取用户详情失败:", error)
      wx.showToast({
        title: "获取用户信息失败",
        icon: "none",
      })
    }
  },

  onUserDetailClose() {
    this.setData({
      showUserDetail: false,
      userDescription: "",
      userWeChatId: "",
      userQQId: "",
    })
  },
})
